import 'dart:io';

import 'package:dio/dio.dart';
import 'package:dio/io.dart';
import 'package:http_proxy/http_proxy.dart';
import 'package:wch/utils/log_utils.dart';

class ProxyHelper {
  // 是否启用代理
  static bool isProxy = true;

  // 设置成功代理IP过，就不再重复设置
  static bool proxyEd = false;

  /// 设置抓包
  static Future<HttpProxy?> setProxy(Dio? dio) async {
    if (!isProxy || proxyEd || dio == null) return null;

    HttpProxy? proxy = await HttpProxy.createHttpProxy();
    if (proxy?.host == null) {
      isProxy = false;
      return null;
    }
    LogUtils.log('setProxy');
    return proxy;
  }
}